<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" charset="utf-8">
    <title> 在JavaScript脚本中通过正则表达式验证用户注册信息</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <script src="js/check.js"></script>
    <body>
    <table width="1014" height="706" border="0" align="center" cellpadding="0" cellspacing="0" background="images/form1.jpg">
      <tr>
        <td width="133" height="228"> </td>
        <td width="750"> </td>
        <td width="113"> </td>
      </tr>
      <tr>
        <td> </td>
        <td align="center" valign="top">
        <form name="form_reg" method="post" action="" onSubmit="return chkreginfo(form_reg,'all')">
          <table width="620" height="262" border="0" align="center" cellpadding="0" cellspacing="0">

                    <tr><!--验证用户名-->
                      <td width="120" height="30"><div align="right">用户名：</div></td>
                      <td colspan="2"> <input type="text" name="recuser" size="20" class="inputcss" onBlur="chkreginfo(form_reg,0)">
                        <font color="#FF0000">*</font>
                      <div id="chknew_recuser" style="color:#FF0000"></div></td>
                    </tr>

                   <!--  -->
            </table>
        </form></td>
        <td> </td>
      </tr>
      <tr>
        <td> </td>
        <td> </td>
        <td> </td>
      </tr>
      <tr>
        <td> </td>
        <td> </td>
        <td> </td>
      </tr>
    </table>
    </body>
    </html>

<script>

       function chkreginfo(form,mark){                 //定义验证用户提交信息的方法，获取方法传递的参数
            if(mark==0 || mark=="all"){                       //判断标签的值是否为空
               if(form.recuser.value==""){                    //判断用户名是否为空
                 chknew_recuser.innerHTML="请输入用户名！";      //如果为空则输出“请输入用户名”
                 form.recuser.style.backgroundColor="#FF0000";    //定义输出文字的颜色
                 return false;                                    //返回false
               }else{
                 chknew_recuser.innerHTML="恭喜用户名可用！";     //如果不为空则输出“恭喜用户名可用”
                 form.recuser.style.backgroundColor="#999999";    //定义输出文字颜色
               }
             }

    function checkregtel(regtel){               //验证手机号码
        var str=regtel;                         //参数传递手机号码变量
        var Expression=/^13(\d{9})$|^15(\d{9})$|^189(\d{8})$/;      //定义正则表达式
        var objExp=new RegExp(Expression);      //类的实例化
        if(objExp.test(str)==true){         //执行验证操作
            return true;                        //符合条件返回true
        }else{
            return false;                       //否则返回false
        }
    }
    function checkregtels(regtels){         //验证座机号码
        var str=regtels;
        var Expression=/^(\d{3}-)(\d{8})$|^(\d{4}-)(\d{7})$|^(\d{4}-)(\d{8})$/;
        var objExp=new RegExp(Expression);
        if(objExp.test(str)==true){
            return true;
        }else{
            return false;
        }
    }
    function checkregemail(emails){         //验证邮箱地址
        var str=emails;
        var Expression=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
        var objExp=new RegExp(Expression);
        if(objExp.test(str)==true){
            return true;
        }else{
            return false;
        }
    }






      if(mark==1 || mark=="all"){
         if(form.address.value==""){
           chknew_address.innerHTML="请输入联系地址！";
           form.address.style.backgroundColor="#FF0000";
           return false;
         }else{
           chknew_address.innerHTML="地址输入成功！";
           form.address.style.backgroundColor="#999999";
         }
       }


     if(mark==2 || mark=="all"){
         if(form.postalcode.value==""){                     //判断邮编是否为空
           chknew_postalcode.innerHTML="请输入邮编！";
           form.postalcode.style.backgroundColor="#FF0000";
           return false;
         }else if(isNaN(form.postalcode.value)){            //判断邮编是否由数字组成
           chknew_postalcode.innerHTML="邮编由数字组成！";
           form.postalcode.style.backgroundColor="#FF0000";
           return false;
         }else if(form.postalcode.value.length!=6){     //判断邮编的字符串长度
           chknew_postalcode.innerHTML="邮编由6位数字组成！";
           form.postalcode.style.backgroundColor="#FF0000";
           return false;
         }else{
           chknew_postalcode.innerHTML="邮编格式正确！";
           form.postalcode.style.backgroundColor="#999999";
         }
       }


       if(mark==3 || mark=="all"){
         if(form.qq.value==""){
           chknew_qq.innerHTML="请输入QQ号码！";
           form.qq.style.backgroundColor="#FF0000";
           return false;
         }else if(isNaN(form.qq.value)){
           chknew_qq.innerHTML="QQ号由数字组成！";
           form.qq.style.backgroundColor="#FF0000";
           return false;
         }else{
           chknew_qq.innerHTML="号码格式正确！";
           form.qq.style.backgroundColor="#999999";
         }
       }

      if(mark==4 || mark=="all"){
         if(form.email.value==""){                              //判断邮箱是否为空
           chknew_email.innerHTML="请输入E-mail地址！";
           form.email.style.backgroundColor="#FF0000";
           return false;
         }else if(!checkregemail(form.email.value)){            //调用定义的checkregemail()方法验证邮箱格式是否正确
                chknew_email.innerHTML="邮箱地址的格式不正确！";
                form.email.style.backgroundColor="#FF0000";
                return false;
           }else{
              chknew_email.innerHTML="邮箱地址格式正确!";
              form.email.style.backgroundColor="#999999";
           }
       }

      if(mark==5 || mark=="all"){
            if(form.mtel.value==""){
                chknew_mtel.innerHTML="请输入电话号码！";
                form.mtel.style.backgroundColor="#FF0000";
                return false;
            }else if(!checkregtel(form.mtel.value)){                //调用checkregtel()方法验证手机号码格式是否正确
                chknew_mtel.innerHTML="电话号码的格式不正确！";
                form.mtel.style.backgroundColor="#FF0000";
                return false;
            }else if(isNaN(form.mtel.value)){
                chknew_mtel.innerHTML="电话号由数字组成！";
                form.mtel.style.backgroundColor="#FF0000";
                return false;
            }else{
                chknew_mtel.innerHTML="号码格式正确！";
                form.mtel.style.backgroundColor="#999999";
            }
        }


       if(mark==6 || mark=="all"){
            if(form.gtel.value==""){
                chknew_gtel.innerHTML="请输入电话号码！";
                form.gtel.style.backgroundColor="#FF0000";
                return false;
            }else if(!checkregtels(form.gtel.value)){
                chknew_gtel.innerHTML="电话号码的格式不正确！";
                form.gtel.style.backgroundColor="#FF0000";
                return false;
            }else{
                chknew_gtel.innerHTML="";
                form.gtel.style.backgroundColor="#999999";
            }
        }


       }
</script>